from django.db.models import Q
from django.shortcuts import render
from user import models
# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from utils.jwttool import tool
from utils.myedis import redistoll
from rest_framework.viewsets import ModelViewSet
from user.tasks import send
from utils import sendmsg
from user.ser import UserService
from utils.Alipay import AliPaySDK
import re
import random

class RegView(APIView):
    def post(self, request):
        phone=str(request.data.get('phone'))
        email=str(request.data.get('email'))
        username=str(request.data.get('username'))
        password=str(request.data.get('password'))
        ret1=re.match(r'^1[3-9]\d{9}$',phone)
        print(ret1)
        ret2=re.match(r'^[0-9a-zA-Z]{6,14}@(qq|sina|163)\.com$',email)
        print(ret2)
        ret3=re.match(r'\d{6}',password)
        print(ret3)
        if ret2 and ret3 and ret1 and username:
            models.UserModel.objects.create(
                username=username, email=email, password=password,phone=phone
            )
            return Response({'message': 'User created successfully','code':200})
        else:
            return Response({'message': 'Missing required fields','code':400})

class LoginPassView(APIView):
    def post(self, request):
        username=request.data.get("username")
        password=request.data.get("password")
        if username and password is None:
            return Response({'message': '参数不正确','code':400})
        data=models.UserModel.objects.filter(Q(username=username)|Q(email=username)|Q(phone=username)).first()
        if data is None:
            return Response({'message': '用户名不正确','code':400})
        else:
            if data.password==password:
                token=tool.getToken(data.id,username)
                return Response({'message': '登陆成功','code':200,"token":token,"user":data.id})
            else:
                return Response({'message': '密码不正确','code':400})

class SendView(APIView):
    def post(self, request):
        phone=request.data.get('phone')
        yzm=random.randint(1000,9999)
        code=redistoll.getstr(phone)
        if code is None:
            sendmsg.send_message(phone,yzm)
            redistoll.setstr(phone, yzm, 60)
            return Response({'message': '验证码已发送','code':200})
        else:
            return Response({'message': '重复获取验证码','code':400})

class LoginView(APIView):
    def post(self, request):
        phone=request.data.get("phone")
        code=request.data.get("code")
        yzm=str(redistoll.getstr(phone))
        info=models.UserModel.objects.filter(phone=phone).first()
        if info is None:
            return Response({'message': '该手机号未注册','code':400})
        if yzm==code:
            token=tool.getToken(info.id,info.phone)
            return Response({'message': '登陆成功','code':200,"token":token,"user":info.id})
        else:
            return Response({'message': '验证码不正确','code':400})

class UserView(APIView):
    def get(self, request):
        id=request.GET.get("id")
        user=models.UserModel.objects.filter(id=id).first()
        Ser=UserService(instance=user)
        return Response(Ser.data)

